package com.zabra.zl;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zabra.zl.base.BaseActivity;
import com.zabra.zl.ui.index_fragment.account.AccountFragment;
import com.zabra.zl.ui.index_fragment.deal.DealFragment;
import com.zabra.zl.ui.index_fragment.index.IndexFragment;
import com.zabra.zl.ui.index_fragment.money.MoneyFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.fragment_main)
    LinearLayout fragmentMain;
    @BindView(R.id.index_icon)
    ImageView indexIcon;
    @BindView(R.id.index_name)
    TextView indexName;
    @BindView(R.id.bt_index_index)
    RelativeLayout btIndexIndex;
    @BindView(R.id.money_icon)
    ImageView moneyIcon;
    @BindView(R.id.money_name)
    TextView moneyName;
    @BindView(R.id.bt_index_money)
    RelativeLayout btIndexMoney;
    @BindView(R.id.deal_icon)
    ImageView dealIcon;
    @BindView(R.id.deal_name)
    TextView dealName;
    @BindView(R.id.bt_index_deal)
    RelativeLayout btIndexDeal;
    @BindView(R.id.account_icon)
    ImageView accountIcon;
    @BindView(R.id.account_name)
    TextView accountName;
    @BindView(R.id.bt_index_account)
    RelativeLayout btIndexAccount;
    private Fragment fragment;
    private List<Fragment> fragmentList;
    private int currentIndex = 0;
    private FragmentManager fragmentManager;
    private static final String CURRENT_FRAGMENT = "STATE_FRAGMENT_SHOW";
    private long exitTime = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;

    }

    @Override
    public void initView() {

        fragment = new Fragment();
        fragmentList = new ArrayList<>();
        fragmentManager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            currentIndex = savedInstanceState.getInt(CURRENT_FRAGMENT, 0);
            fragmentList.removeAll(fragmentList);
            fragmentList.add(fragmentManager.findFragmentByTag(0 + ""));
            fragmentList.add(fragmentManager.findFragmentByTag(1 + ""));
            fragmentList.add(fragmentManager.findFragmentByTag(2 + ""));
            restoreFragment();
        } else {
            fragmentList.add(new IndexFragment());
            fragmentList.add(new MoneyFragment());
            fragmentList.add(new DealFragment());
            fragmentList.add(new AccountFragment());
            showFragment();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(CURRENT_FRAGMENT, currentIndex);
        super.onSaveInstanceState(outState);
    }

    private void showFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (!fragmentList.get(currentIndex).isAdded()) {
            transaction
                    .hide(fragment)
                    .add(R.id.fragment_main, fragmentList.get(currentIndex), "" + currentIndex);
        } else {
            transaction
                    .hide(fragment)
                    .show(fragmentList.get(currentIndex));
        }
        fragment = fragmentList.get(currentIndex);
        transaction.commit();
    }

    private void restoreFragment() {
        FragmentTransaction mBeginTreansaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == currentIndex) {
                mBeginTreansaction.show(fragmentList.get(i));
            } else {
                mBeginTreansaction.hide(fragmentList.get(i));
            }
        }
        mBeginTreansaction.commit();
        fragment = fragmentList.get(currentIndex);

    }


    @OnClick({R.id.bt_index_index, R.id.bt_index_money, R.id.bt_index_deal, R.id.bt_index_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_index_index:
                currentIndex = 0;
                showFragment();
                indexName.setTextColor(getResources().getColor(R.color.indexSelect));
                moneyName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                dealName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                accountName.setTextColor(getResources().getColor(R.color.indexUnSelect));

                indexIcon.setImageResource(R.mipmap.index1);
                moneyIcon.setImageResource(R.mipmap.money0);
                dealIcon.setImageResource(R.mipmap.deal0);
                accountIcon.setImageResource(R.mipmap.account0);
                break;
            case R.id.bt_index_money:
                currentIndex = 1;
                showFragment();
                indexName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                moneyName.setTextColor(getResources().getColor(R.color.indexSelect));
                dealName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                accountName.setTextColor(getResources().getColor(R.color.indexUnSelect));

                indexIcon.setImageResource(R.mipmap.index0);
                moneyIcon.setImageResource(R.mipmap.money1);
                dealIcon.setImageResource(R.mipmap.deal0);
                accountIcon.setImageResource(R.mipmap.account0);
                break;
            case R.id.bt_index_deal:
                currentIndex = 2;
                showFragment();
                indexName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                moneyName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                dealName.setTextColor(getResources().getColor(R.color.indexSelect));
                accountName.setTextColor(getResources().getColor(R.color.indexUnSelect));

                indexIcon.setImageResource(R.mipmap.index0);
                moneyIcon.setImageResource(R.mipmap.money0);
                dealIcon.setImageResource(R.mipmap.deal1);
                accountIcon.setImageResource(R.mipmap.account0);
                break;
            case R.id.bt_index_account:
                currentIndex = 3;
                showFragment();
                indexName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                moneyName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                dealName.setTextColor(getResources().getColor(R.color.indexUnSelect));
                accountName.setTextColor(getResources().getColor(R.color.indexSelect));

                indexIcon.setImageResource(R.mipmap.index0);
                moneyIcon.setImageResource(R.mipmap.money0);
                dealIcon.setImageResource(R.mipmap.deal0);
                accountIcon.setImageResource(R.mipmap.account1);
                break;
        }
    }

}
